package com.igeekhome.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeekhome.biz.IBlackListService;
import com.igeekhome.biz.ICustomerInfoService;
import com.igeekhome.biz.ICustomerServiceService;
import com.igeekhome.biz.IUnsessionService;
import com.igeekhome.dao.CustomerInfoMapper;
import com.igeekhome.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 客户信息 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-16
 */
@Controller
@RequestMapping("/customerInfo")
public class CustomerInfoController {
    @Autowired
    private  ICustomerInfoService iCustomerInfoService;
    @Autowired
    private ICustomerServiceService iCustomerServiceService;
    @Autowired
    private IUnsessionService iUnsessionService;
    @Autowired
    private IBlackListService iBlackListService;

    @RequestMapping("/Customer")
    public String customerinfo(Integer current, Integer size, HttpSession session, CustomerInfo customerInfo)
    {
        IPage<CustomerInfo> page=new Page<>(current,size);
        IPage<CustomerInfo> page1 = this.iCustomerInfoService.page(page);
        List<CustomerInfo> list = page1.getRecords();
        long pagesCount = page1.getPages();
        session.setAttribute("list",list);
        session.setAttribute("pagesCount",pagesCount);
        session.setAttribute("current",current);
        session.setAttribute("size",size);
        return "/Customer";
    }
    @RequestMapping(value = "/Customer-login",method = RequestMethod.POST)
    public String login(CustomerInfo customerInfo, Model model, HttpSession session){

        String path = "";

//        String md5Pass = DigestUtils.md5DigestAsHex(customerService.getPassword().getBytes());
//        customerService.setPassword(md5Pass);

        QueryWrapper<CustomerInfo> queryWrapper = new QueryWrapper();
        queryWrapper.eq("email",customerInfo.getEmail());
      CustomerInfo customerInfo1 = iCustomerInfoService.getOne(queryWrapper);
      //数据统计
        QueryWrapper<CustomerService> queryWrapper1 = new QueryWrapper();
        QueryWrapper<CustomerInfo> queryWrapper2 = new QueryWrapper();
        queryWrapper1.eq("state","在线");
        queryWrapper2.eq("state","在线");
        session.setAttribute("CustomerNum",iCustomerInfoService.count());
        session.setAttribute("OnlineCustomerNum",iCustomerInfoService.count(queryWrapper2));
        session.setAttribute("CustomerServiceNum",iCustomerServiceService.count());
        session.setAttribute("OnlineCustomerServiceNum",iCustomerServiceService.count(queryWrapper1));
        queryWrapper2.clear();
        queryWrapper2.eq("channel","客服录入");
        session.setAttribute("i",iCustomerInfoService.count(queryWrapper2));

        System.out.println(iCustomerInfoService.count(queryWrapper2));


        queryWrapper2.clear();
        queryWrapper2.eq("channel","网页介入");
        session.setAttribute("j",iCustomerInfoService.count(queryWrapper2));


        if(customerInfo1 != null){

            session.setAttribute("customerInfo1",customerInfo1);
            // session.setAttribute("pagesCount");
            LocalDateTime logintime = LocalDateTime.now();
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.set("state","在线");
            updateWrapper.set("updatetime",logintime);
            updateWrapper.eq("email",customerInfo1.getEmail());
            iCustomerInfoService.update(customerInfo1,updateWrapper);

            path = "redirect:/Customer-index";
        }else {

//            path = "/login";//1
//            model.addAttribute("msg","用户名或密码错误");
//            path = "forward:/login";//2 转发给其他控制器， 在根下找/login 执行
            session.setAttribute("message","邮箱错误！");

            path = "redirect:/Customer-login";
        }

        return path;
    }
    @RequestMapping(value = "/Customer-register",method = RequestMethod.POST)
    public String register(CustomerInfo customerInfo, Model model, HttpSession session){

        session.removeAttribute("emailmsg");
        QueryWrapper<CustomerInfo> queryWrapper = new QueryWrapper();
        queryWrapper.eq("email",customerInfo.getEmail());
        CustomerInfo customerInfo1 = iCustomerInfoService.getOne(queryWrapper);
        if(customerInfo1!= null)
        {
            session.setAttribute("emailmsg","该邮箱已被注册过");
            return "/Customer-register";

        }else if(customerInfo.getEmail().equals("")||customerInfo.getRealname().equals("")){

            session.setAttribute("emailmsg","注册信息不能为空");
            return "/Customer-register";
        }
        else {
            customerInfo.setChannel("网页介入");
            boolean i = iCustomerInfoService.save(customerInfo);
            session.setAttribute("emailmsg","注册成功");
            return "/Customer-login";
        }

    }
    @RequestMapping(value="/CustomerPersonal",method = RequestMethod.POST)
    public  String UpdateAndShow(HttpSession session,CustomerInfo customerInfo)
    {
        CustomerInfo ci=new CustomerInfo();
        ci= (CustomerInfo) session.getAttribute("customerInfo1");
        CustomerInfo customerInfoPerson = new CustomerInfo();
        customerInfoPerson=this.iCustomerInfoService.getById(ci.getId());
        customerInfoPerson.setRealname(customerInfo.getRealname());
        customerInfoPerson.setEmail(customerInfo.getEmail());
        customerInfoPerson.setPhone(customerInfo.getPhone());
        customerInfoPerson.setCompany(customerInfo.getCompany());
        customerInfoPerson.setAddress(customerInfo.getAddress());
        this.iCustomerInfoService.updateById(customerInfoPerson);
        session.setAttribute("customerInfo1",customerInfoPerson);
        return "/CustomerPersonal";
    }
    @RequestMapping("/CustomerServiceChoose")
    public String CustomerServiceChoose(Integer current,Integer size,Model model,CustomerService cs,HttpSession session)
    {
        //分页查询
        IPage<CustomerService> page=new Page<>(current,size);
        IPage<CustomerService> customerServiceIPage=this.iCustomerServiceService.page(page);
        List<CustomerService> customerServiceList=customerServiceIPage.getRecords();
        long pagescount=customerServiceIPage.getPages();
        model.addAttribute("pagescount",pagescount);
        model.addAttribute("customerservicelist",customerServiceList);
        model.addAttribute("current", current);
        model.addAttribute("size", size);
        return "CustomerServiceChoose";
    }

    @RequestMapping( value = "/Service")
    public String toService(HttpSession session,CustomerService cs,CustomerInfo cif,Unsession us)
    {
        CustomerInfo customerInfo;
        if(cs!=null) {

            customerInfo = (CustomerInfo) session.getAttribute("customerInfo1");
            CustomerInfo ci;
            ci = this.iCustomerInfoService.getById(customerInfo);
            QueryWrapper<CustomerService> customerServiceQueryWrapper = new QueryWrapper<>();
            customerServiceQueryWrapper.eq("customerserviceid", cs.getCustomerserviceid());
            CustomerService customerService = iCustomerServiceService.getOne(customerServiceQueryWrapper);
            session.setAttribute("chooseservice", customerService);
            session.setAttribute("csid",customerService.getCustomerserviceid());
            if(cs.getCustomerserviceid()!=null)
            {
                //修改客户个人信息
                ci.setRealname(cif.getRealname());
                ci.setCustomerserviceid(customerService.getCustomerserviceid());
                ci.setCompany(cif.getCompany());
                this.iCustomerInfoService.updateById(ci);
                //提交新的工单
                Unsession unsession = new Unsession();
                LocalDateTime logintime = LocalDateTime.now();
                unsession.setCustomerserviceid(customerService.getCustomerserviceid());
                unsession.setCustomerid(ci.getId());
                unsession.setRequest(us.getRequest());
                unsession.setCreatetime(logintime);
                this.iUnsessionService.save(unsession);
            }
            else
            {
                session.setAttribute("tips","请选择客服！");
            }


        }
        else
        {
           session.setAttribute("csid","");
        }
        return "Service";

    }
    @RequestMapping("/Delete")
    public String delete(CustomerInfo customerInfo,int current, int size, HttpSession session){

        this.iCustomerInfoService.removeById(customerInfo.getId());

        IPage<CustomerInfo> page=new Page<>(current,size);
        IPage<CustomerInfo> page1 = this.iCustomerInfoService.page(page);
        List<CustomerInfo> list = page1.getRecords();
        long pagesCount = page1.getPages();
        session.setAttribute("list",list);
        session.setAttribute("pagesCount",pagesCount);
        session.setAttribute("current",current);
        session.setAttribute("size",size);
        return "Customer";
    }
    @RequestMapping("/Update")
    public String update(CustomerInfo customerInfo,HttpSession session){

        customerInfo=this.iCustomerInfoService.getById(customerInfo);
        session.setAttribute("ci",customerInfo);
        return "CustomerUpdate";
    }

    @RequestMapping( value = "/ciUpdate",method = RequestMethod.POST)
    public String ciUpdate(HttpSession session,CustomerInfo ci) {



        CustomerInfo ci1 = (CustomerInfo) session.getAttribute("ci");
        ci1.setRealname(ci.getRealname());
        ci1.setEmail(ci.getEmail());
        ci1.setPhone(ci.getPhone());
        ci1.setCompany(ci.getCompany());
        ci1.setAddress(ci.getAddress());
        ci1.setLevel(ci.getLevel());
        ci1.setChannel(ci.getChannel());
        ci1.setRemark(ci.getRemark());
        this.iCustomerInfoService.updateById(ci1);
        session.setAttribute("ci",ci1);
        return "CustomerUpdate";
    }
//    @RequestMapping("/Black")
//    public String black(CustomerInfo customerInfo,HttpSession session,BlackList blackList){
//
//
//        customerInfo=this.iCustomerInfoService.getById(customerInfo);
//        BlackList blackList=new BlackList(
//                customerInfo.getCustomerserviceid(),
//                customerInfo.getId(),
//                "aaaa",
//                LocalDateTime.now(),
//                "123.23.23.3",
//                null
//        );
//
//        this.iBlackListService.save(blackList);
//
//        IPage<BlackList> page=new Page<>(1,8);
//        IPage<BlackList> page1 = this.iBlackListService.page(page);
//        long pagesCount = page1.getPages();
//
//        IPage<BlackList> page2=new Page<>(pagesCount,8);
//        IPage<BlackList> page3 = this.iBlackListService.page(page2);
//        List<BlackList> list = page3.getRecords();
//        pagesCount = page1.getPages();
//        session.setAttribute("list",list);
//        session.setAttribute("pagesCount",pagesCount);
//        session.setAttribute("current",pagesCount);
//        session.setAttribute("size",8);
//        return "Blacklist";
//    }

}

